//
//  STBasicPositionViewController.m
//  BasicAnimation-OC
//
//  Created by Stan on 2017-02-09.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STBasicPositionViewController.h"

@interface STBasicPositionViewController ()<CAAnimationDelegate>
@property(weak,nonatomic)CALayer * redLayer;
@property(weak,nonatomic)CALayer * blueLayer;
@property(weak,nonatomic)CALayer * grayLayer;
@property(weak,nonatomic)CALayer * greenLayer;

@end

@implementation STBasicPositionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    //    创建红色线性运动的Layer
    self.redLayer = [self createLayerWithPosition:CGPointMake(0, 150) backgroundColor:[UIColor redColor]];
    [self.redLayer addAnimation:[self createBasicAnimationWithFromValue:CGPointMake(0, 150) toValue:CGPointMake(300, 150) timingFunction:kCAMediaTimingFunctionLinear] forKey:@"linear"];
    
    //    创建蓝色渐进运动的Layer
    self.blueLayer = [self createLayerWithPosition:CGPointMake(0, 250) backgroundColor:[UIColor blueColor]];
    [self.blueLayer addAnimation:[self createBasicAnimationWithFromValue:CGPointMake(0, 250) toValue:CGPointMake(300, 250) timingFunction:kCAMediaTimingFunctionEaseIn] forKey:@"easeIn"];
    
    //    创建灰色渐出运动的Layer
    self.grayLayer = [self createLayerWithPosition:CGPointMake(0, 350) backgroundColor:[UIColor grayColor]];
    [self.grayLayer addAnimation:[self createBasicAnimationWithFromValue:CGPointMake(0, 350) toValue:CGPointMake(300, 350) timingFunction:kCAMediaTimingFunctionEaseOut] forKey:@"easeOut"];
    
    //    创建绿色渐进渐出运动的Layer
    self.greenLayer = [self createLayerWithPosition:CGPointMake(0, 450) backgroundColor:[UIColor greenColor]];
    [self.greenLayer addAnimation:[self createBasicAnimationWithFromValue:CGPointMake(0, 450) toValue:CGPointMake(300, 450) timingFunction:kCAMediaTimingFunctionEaseInEaseOut] forKey:@"easeInAndEaseOut"];
}


#pragma 实现代理协议的方法

//动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim{
    
}




//动画结束的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.redLayer removeAnimationForKey:@"linear"];
    
    [self.blueLayer removeAnimationForKey:@"easeIn"];

    [self.grayLayer removeAnimationForKey:@"easeOut"];

    [self.greenLayer removeAnimationForKey:@"easeInAndEaseOut"];

}

#pragma 抽取创建动画及Layer的公共方法

//创建CALayer
- (CALayer *)createLayerWithPosition:(CGPoint)position backgroundColor:(UIColor *)backgroundColor{
    //创建CALayer
    CALayer *layer = [CALayer layer];
    
    //设置位置和大小
    layer.position = position;
    layer.bounds = CGRectMake(0, 0, 100, 100);
    
    
    //设置背景颜色
    layer.backgroundColor = backgroundColor.CGColor;
    
    //把layer添加到UIView的layer上
    [self.view.layer addSublayer:layer];
    
    return layer;
}

//创建动画
- (CABasicAnimation *)createBasicAnimationWithFromValue:(CGPoint)fromValue toValue:(CGPoint)toValue timingFunction:(NSString *)timingFunction{
    //创建动画对象
    CABasicAnimation *basicAni = [CABasicAnimation animation];
    
    //设置动画属性
    basicAni.keyPath = @"position";
    
    //设置动画的起始位置。也就是动画从哪里到哪里
    basicAni.fromValue = [NSValue valueWithCGPoint:fromValue];
    
    //动画结束后，layer所在的位置
    basicAni.toValue = [NSValue valueWithCGPoint:toValue];
    
    //动画持续时间
    basicAni.duration = 2;
    
    //动画重复次数
    basicAni.repeatCount = CGFLOAT_MAX;
    
    //xcode8.0之后需要遵守代理协议
    basicAni.delegate = self;
    
    basicAni.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];

    return basicAni;
}

@end
